package com.inei.evaluacionmype.controller.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.inei.evaluacionmype.R;
import com.inei.evaluacionmype.controller.ResultadoController;
import com.inei.evaluacionmype.model.entity.Resultado;

import android.R.menu;
import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class Menu_Activity extends Activity {

	// componentes
	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle mDrawerToggle;
	private ListView mDrawerList;

	// variables
	private String ruc_id;
	private String[] items;
	private String[] titulos;
	private String titulo;
	private int cap;
	private CapituloI_Activity cap01;
	private CapituloII_Activity cap02;
	private CapituloIII_Activity cap03;
	private CapituloIV_Activity cap04;
	private Observaciones_Activity obs;
	private boolean activo = false;
	private int ini = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);

		IniciarComponentes();

		CargarDatos();

		AgregarEventos();

		if (savedInstanceState == null) {
			SelectItem(0);
			cap = 0;
		}

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(titulo);
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle("SELECCIONAR CAPITULO");
				invalidateOptionsMenu();
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_cap, menu);
		menu.getItem(1).setVisible(false);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (mDrawerList.isShown()) {
			menu.setGroupVisible(R.id.gpMenu, false);
			ini = 0;
		} else {
			menu.setGroupVisible(R.id.gpMenu, true);
			if (ini == 1) {
				if (activo) {
					menu.findItem(R.id.mnIniciar).setVisible(false);
					menu.findItem(R.id.mnTerminar).setVisible(true);
					activo = false;
				} else {
					menu.findItem(R.id.mnIniciar).setVisible(true);
					menu.findItem(R.id.mnTerminar).setVisible(false);
					activo = true;
				}
			} else {
				ini = 1;
				if (!activo) {
					menu.findItem(R.id.mnIniciar).setVisible(false);
					menu.findItem(R.id.mnTerminar).setVisible(true);
				} else {
					menu.findItem(R.id.mnIniciar).setVisible(true);
					menu.findItem(R.id.mnTerminar).setVisible(false);
				}
			}
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		switch (item.getItemId()) {
		case R.id.mnIniciar:
			invalidateOptionsMenu();
			break;

		case R.id.mnTerminar:
			CrearMenuR();
			break;

		case R.id.mnValidar:
			Validar();
			break;

		case R.id.mnGuardar:
			GuardarDatos();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public void setTitle(CharSequence titulo) {
		getActionBar().setTitle(titulo);
	}

	@Override
	public void onBackPressed() {
		Intent ir_marco = new Intent(Menu_Activity.this, Marco_Activity.class);
		startActivity(ir_marco);

		finish();
		super.onBackPressed();
	}

	private void IniciarComponentes() {
		mDrawerLayout = (DrawerLayout) findViewById(R.id.menu);

		mDrawerList = (ListView) findViewById(R.id.lvMenu);

		items = getResources().getStringArray(R.array.menu_item);
		mDrawerList.setAdapter(new ArrayAdapter<String>(this,
				R.layout.menu_item, items));

		titulos = getResources().getStringArray(R.array.titulo_item);
	}

	private void CargarDatos() {
		ruc_id = getIntent().getExtras().getString("ruc_id");
	}

	private void AgregarEventos() {
		mDrawerList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos,
					long id) {
				SelectItem(pos);
			}
		});
	}

	private void SelectItem(int pos) {
		titulo = titulos[pos];
		setTitle(titulo);
		Fragment fragment = null;
		switch (pos) {
		case 0:
			cap01 = new CapituloI_Activity();
			fragment = cap01;
			break;
		case 1:
			cap02 = new CapituloII_Activity();
			fragment = cap02;
			break;
		case 2:
			cap03 = new CapituloIII_Activity();
			fragment = cap03;
			break;
		case 3:
			cap04 = new CapituloIV_Activity();
			fragment = cap04;
			break;
		case 4:
			obs = new Observaciones_Activity();
			fragment = obs;

		default:
			break;
		}
		FragmentManager fragmentManager;
		Bundle datos;
		datos = new Bundle();
		datos.putString("ruc_id", ruc_id);
		fragment.setArguments(datos);
		fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.contenido_menu, fragment).commit();

		mDrawerList.setItemChecked(pos, true);
		mDrawerLayout.closeDrawer(mDrawerList);
		cap = pos;
	}

	private void CrearMenuR() {
		final Dialog dialogoR = new Dialog(this,
				R.style.Theme_Dialog_Translucent);
		dialogoR.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogoR.setContentView(R.layout.dialog_next);

		TextView titulo = (TextView) dialogoR.findViewById(R.id.titulo);
		titulo.setText("Evaluador");

		TimePicker tp1R = (TimePicker) dialogoR.findViewById(R.id.tp1);
		tp1R.setIs24HourView(true);

		DatePicker dp1R = (DatePicker) dialogoR.findViewById(R.id.dp1);

		final Spinner spOpcionesR = (Spinner) dialogoR
				.findViewById(R.id.spOpciones);

		final EditText etO = (EditText) dialogoR.findViewById(R.id.etO);
		etO.setVisibility(View.GONE);

		ResultadoController conRes = new ResultadoController(
				getApplicationContext());
		List<Resultado> opciones = conRes.ResultadoObtenerTodo();
		Resultado res = new Resultado();
		res.setCODIGO("0");
		res.setNOMRESULTADO("Seleccionar resultado");
		opciones.add(0, res);
		spOpcionesR.setAdapter(new ArrayAdapter<Resultado>(this,
				android.R.layout.simple_spinner_dropdown_item, opciones));

		spOpcionesR.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> a, View v, int pos,
					long id) {
				switch (pos) {
				case 8:
					etO.setVisibility(View.VISIBLE);
					break;

				default:
					etO.setText("");
					etO.setVisibility(View.GONE);
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		Button btn1 = (Button) dialogoR.findViewById(R.id.btn1);

		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				invalidateOptionsMenu();
				dialogoR.dismiss();
			}
		});

		Button btn2 = (Button) dialogoR.findViewById(R.id.btn2);

		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialogoR.dismiss();
			}
		});

		dialogoR.show();

	}

	private void Validar() {
		switch (cap) {
		case 0:
			cap01.ValidarCap();
			break;

		case 1:
			cap02.ValidarCap();
			break;

		case 2:
			cap03.ValidarCap();
			break;

		case 3:
			cap04.ValidarCap();
			break;

		default:
			break;
		}
	}

	private void GuardarDatos() {
		switch (cap) {
		case 0:
			cap01.GuardarCap();
			break;

		case 1:
			cap02.GuardarCap();
			break;

		case 2:
			cap03.GuardarCap();
			break;

		case 3:
			cap04.GuardarCap();
			break;

		default:
			break;
		}
	}
}
